package com.hnd.springframerwork.beans.factory.registry;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.hnd.springframerwork.beans.BeansException;
import com.hnd.springframerwork.beans.PropertyValue;
import com.hnd.springframerwork.beans.PropertyValues;
import com.hnd.springframerwork.beans.factory.config.BeanDefinition;
import com.hnd.springframerwork.beans.factory.config.BeanReference;
import com.hnd.springframerwork.core.io.Resource;
import com.hnd.springframerwork.core.io.ResourceLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.io.InputStream;

/**
 * 获取xml文件流，解析xml文件，将文件配置注入到容器中
 */
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
    public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
        super(registry);
    }

    public XmlBeanDefinitionReader(BeanDefinitionRegistry registry, ResourceLoader resourceLoader) {
        super(registry, resourceLoader);
    }

    @Override
    public void loadBeanDefinition(String... locations) throws BeansException {
        for (String location : locations) {
            loadBeanDefinition(location);
        }
    }

    @Override
    public void loadBeanDefinition(Resource resource) {
        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
            doLoadBeanDefinition(inputStream);

        } catch (IOException | ClassNotFoundException e) {
            throw new BeansException("实例化失败", e);
        }

    }

    @Override
    public void loadBeanDefinition(Resource... resources) {
        for (Resource resource : resources) {
            loadBeanDefinition(resource);
        }
    }

    @Override
    public void loadBeanDefinition(String location) {
        Resource resource = getResourceLoader().getResource(location);
        loadBeanDefinition(resource);

    }

    private void doLoadBeanDefinition(InputStream inputStream) throws ClassNotFoundException {
        Document document = XmlUtil.readXML(inputStream);
        Element beans =  document.getDocumentElement();


        NodeList childNodes = beans.getChildNodes();

         for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if (!(item instanceof Element)) {
                continue;
            }
            if (!"bean".equals(item.getNodeName())) {
                continue;
            }
            Element element = (Element) item;
            String name = element.getAttribute("name");
            String className = element.getAttribute("class");
            Class<?> clazz = Class.forName(className);

            NodeList childNodes1 = element.getChildNodes();
            PropertyValues pvs = new PropertyValues();
            for (int j = 0; j < childNodes1.getLength(); j++) {

                Node item1 = childNodes1.item(j);

                if (!(item1 instanceof Element)) {
                    continue;
                }
                if (!"property".equals(item1.getNodeName())) {
                    continue;
                }
                Element property = (Element) item1;
                String propertyName = property.getAttribute("name");
                String ref = property.getAttribute("ref");
                System.out.println(ref.length());
                Object value = StrUtil.isNotEmpty(property.getAttribute("ref"))
                        ? new BeanReference(property.getAttribute("ref"))
                        :property.getAttribute("value") ;
                pvs.addProperty(new PropertyValue(propertyName, value));
            }
            BeanDefinition beanDefinition = new BeanDefinition(name, clazz, pvs);
            getRegistry().registryBeanDefinition(name, beanDefinition);
        }
    }
}
